package com.example.hongyu.avtt.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;


import com.example.hongyu.avtt.R;
import com.example.hongyu.avtt.config.Constant;
import com.example.hongyu.avtt.event.Event;
import com.example.hongyu.avtt.listener.OnItemClickListener;
import com.example.hongyu.avtt.model.Txt;
import com.example.hongyu.avtt.model.entity.BaseResponse;
import com.example.hongyu.avtt.network.BaseObserver;
import com.example.hongyu.avtt.network.RetrofitHelper;
import com.example.hongyu.avtt.network.RxUtil;
import com.example.hongyu.avtt.network.api.AppInfoApi;
import com.example.hongyu.avtt.ui.activity.TxtReadActivity;
import com.example.hongyu.avtt.ui.adapter.TxtListAdapter;
import com.example.hongyu.avtt.ui.base.BaseFragment;
import com.example.hongyu.avtt.ui.widget.ToastTool;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class TxtFragment extends BaseFragment {
    private final String TAG="VideoFragment";
    @BindView(R.id.recycle_view)
    XRecyclerView xRecyclerView;

    TxtListAdapter txtListAdapter;
    List<Txt> txts=new ArrayList<>();
    private int pageIndex=1;
    private int pageSize=40;

    //判断是否已进行过加载，避免重复加载
    private boolean isLoad=false;
    //判断当前fragment是否可见
    private boolean isVisibleToUser = false;
    //判断当前fragment是否回调了resume
    private boolean isResume = false;
    int dataSize=0;
    @Override
    protected View getContentView(LayoutInflater inflater, @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.fragment_txt, container, false);
    }

    @Override
    protected void initView() {

        xRecyclerView.setLayoutManager(new GridLayoutManager(this.getContext(),4));
        txtListAdapter=new TxtListAdapter(this.getContext(),txts);
        xRecyclerView.setAdapter(txtListAdapter);
        xRecyclerView.setPullRefreshEnabled(true);
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                pageIndex=1;
                htppGetHtmlList(pageIndex, true);
            }

            @Override
            public void onLoadMore() {
                pageIndex++;
                htppGetHtmlList(pageIndex, false);
            }
        });
        txtListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                ToastTool.show(""+position);
                Txt txt=txts.get(position-1);
                String url=txt.getPath();
                String name=txt.getTitle();
                int id=txt.getId();
                mBundle=getBundle();
                if (mBundle==null){
                    mBundle=new Bundle();
                }
                mBundle.putString("PLAYURL",url);
                mBundle.putString("PLAYNAME",name);
                mBundle.putInt("PlayId",id);
                sendIntent(TxtReadActivity.class,mBundle);

            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onEventProcess(Event event) {
        int code=event.getCode();
        switch (code){
            case Constant.REFRESH_PAGE_DATA_EVENT:
                pageIndex=1;
                htppGetHtmlList(pageIndex, true);
                break;
        }

    }
    @SuppressLint("AutoDispose")
    private void htppGetHtmlList(int index, boolean isRefresh) {
        RetrofitHelper.create(AppInfoApi.class)
                .txtList("","",pageIndex,pageSize)
                .compose(RxUtil.observervableToMain())
                .subscribe( new BaseObserver<List<Txt>>(getLoadingDialog()){
                    @Override
                    public void onSuccess(BaseResponse<List<Txt>> response) {
                        if (response!=null&&response.getData()!=null){
                            if(isRefresh){
                                txts.clear();
                                xRecyclerView.refreshComplete();
                            }else {
                                dataSize=response.getData().size();
                                xRecyclerView.loadMoreComplete();


                            }

                            txts.addAll(response.getData());

                            txtListAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if(isRefresh){
                            xRecyclerView.refreshComplete();
                        }else {
                            xRecyclerView.loadMoreComplete();
                        }
                    }
                });
    }
    @Override
    public void onResume() {
        super.onResume();
        isResume=true;
        lazyLoad();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.i(TAG, "setUserVisibleHint: "+isVisibleToUser+""+this);
        this.isVisibleToUser=isVisibleToUser;
        lazyLoad();
    }

    private void lazyLoad() {
        if (!isLoad&&isVisibleToUser&&isResume){
            htppGetHtmlList(pageIndex,true);
            isLoad=true;
        }
    }
}
